package com.gdut.simple.presenter.user;

import com.gdut.simple.R;
import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.NetworkUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.RegisterContract;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.IRegisterModel;
import com.gdut.simple.model.impl.RegisterModelImpl;

public class RegisterPresenter implements RegisterContract.Presenter {

    private RegisterContract.View mRegisterView;
    private IRegisterModel mRegisterModel;

    public RegisterPresenter(RegisterContract.View view) {
        this.mRegisterView = view;
        mRegisterModel = new RegisterModelImpl();
        mRegisterView.setPresenter(this);
    }

    @Override
    public void register() {
        if (!NetworkUtils.isConnected(BasicApplication.getApplication())) {
            mRegisterView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_error_net));
            return;
        }
        String account = mRegisterView.getAccount();
        String password = mRegisterView.getPassword();
        String password2 = mRegisterView.getPassword2();
        if (account.isEmpty() || password.isEmpty() || password2.isEmpty()) {
            mRegisterView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_null_acc_psw))
            ;
            return;
        }
        if (!password.equals(password2)) {
            mRegisterView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_incorrect_psw))
            ;
            return;
        }
        mRegisterView.showProgress(BasicApplication.getApplication().getResources().getString(R.string.msg_register))
        ;
        mRegisterModel.register(account, password, new RequestCallback<User>() {
            @Override
            public void onSuccess(User user) {
                mRegisterView.hideProgress();
                mRegisterView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_register_success))
                ;
                mRegisterView.next();
            }

            @Override
            public void onFailure(String message) {
                mRegisterView.hideProgress();
                mRegisterView.showMessage(message);
            }
        });
    }

}
